package com.deke.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deke.entity.TbMember;
import com.deke.student.service.TbMenberService;
import com.deke.student.utils.SMSUtils;
import com.deke.student.vo.QianLoginVo;
import com.deke.student.vo.TbMenberVo;
import com.deke.student.vo.UpTbMenberVo;
import com.deke.utils.RandomCode;
import com.deke.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author ：陈昂（731712841@qq.com）
 * @date ：Created in 2022/12/6 12:16
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/student/member")
@Api(tags = "会员接口类")
public class TbMemberController {
    @Autowired
    private TbMenberService tbMenberService;

    @Autowired
    private SMSUtils smsUtils;

    @Autowired
    private RedisTemplate<String,String> template;

    @ApiOperation(value = "分页查询+条件搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "currentPage"),
            @ApiImplicitParam(value = "当页数量", name = "pageSize"),
            @ApiImplicitParam(value = "会员搜索对象", name = "TbMenberVo")
    })
    @PostMapping("/getStudentAll/{currentPage}/{pageSize}")
    public Result<IPage<TbMember>> getStudentAll(@PathVariable(required = true) Integer currentPage, @PathVariable(required = true) Integer pageSize, @RequestBody TbMenberVo tbMenberVo) {

        return tbMenberService.queryByConditionPage(currentPage, pageSize, tbMenberVo);

    }

    @ApiOperation(value = "编辑接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "会员编辑编辑对象", name = "UpTbMenberVo")
    })
    @PostMapping("modifyStudentInformation")
    public Result<TbMember> modifyStudentInformation(@RequestBody UpTbMenberVo upTbMenberVo) {
        return tbMenberService.UPdateTbMenber(upTbMenberVo);
    }

    @ApiOperation(value = "状态更改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "状态", name = "isDisabled"),
            @ApiImplicitParam(value = "id", name = "id")
    })
    @PostMapping("/stateChange/{isDisabled}/{id}")
    public Result stateChange(@PathVariable Integer isDisabled, @PathVariable String id) {
        return tbMenberService.UPdateTbMenberState(id, isDisabled);
    }



    @ApiOperation(value = "注册短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "mobile"),
    })
    @GetMapping("/getYanZhengMa/{mobile}")
    public Result sendCode(@PathVariable(value = "mobile") String phone) {
        if (template.hasKey(phone)) {
            throw new RuntimeException("不允许重复发送");
        }
        String code = RandomCode.YanZhengMa(4);
        smsUtils.sendMsg(phone, code);
        template.opsForValue().set(phone, code, 60, TimeUnit.SECONDS);
        return new Result(2000, "验证码", code);
    }



    /**
     * @return :  
     * @create by: Username 刘亦辰 
     * @description: 用户注册 添加用户信息 
     * @create time: 2022/12/17 16:30
     **/
    @PostMapping("/installMember/{mobile}/{qianPassword}")
    public Result installMember(@PathVariable String mobile, @PathVariable String qianPassword) {
        return tbMenberService.installMember(mobile, qianPassword);
    }


    /**
     * @return :  
     * @create by: Username 刘亦辰 
     * @description: 前端登录 
     * @create time: 2022/12/17 18:18
     **/
    @PostMapping("/qianLogin")
    public Result qianLogin(@RequestBody QianLoginVo qianLoginVo) {
        return tbMenberService.qianLogin(qianLoginVo);
    }

    @ApiOperation(value = "根据ID查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id")
    })
    @PostMapping("/queryAllMemberById/{id}")
    public TbMember queryAl1MemberById(@PathVariable String id) {
        return tbMenberService.selectQueryAllId(id);
    }

    @ApiOperation(value = "根据手机号查询用户信息")
    @ApiImplicitParams({
            //@ApiImplicitParam(value = "mobile" ,name = "手机号")
    })
    @GetMapping("findStudentBymobile/{mobile}")
    public TbMember findStudentBymobile(@PathVariable String mobile) {
        return tbMenberService.selectQueryAllMobile(mobile);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("updateMemberPassword/{mobile}/{newPassword}")
    public Result updateMemberPassword(@PathVariable String mobile, @PathVariable String newPassword) {
        return tbMenberService.UPdateTbMenberPassword(mobile, newPassword);

    }

}
